#include "CSetSpriteAnimCommand.h"
#include "CUserInterface.h"
#include "CLevelEditor.h"
#include "CAnimatedBillboardSceneNode.h"

extern CGlobals* g;
extern CUserInterface* ui;
extern CLevelEditor* editor;

CSetSpriteAnimCommand::CSetSpriteAnimCommand(int id, s32 newAnim)
{
	m_id = id;
	CSceneObject* obj = editor->getSceneObjectFromID(id);
	m_oldAnim = obj->m_UserData->getAttributeAsInt("CurrentAnim");
	m_newAnim = newAnim;
	setDescription(L"change SpriteAnim");
}

void CSetSpriteAnimCommand::redo()
{
	CSceneObject* obj = editor->getSceneObjectFromID(m_id);
	obj->m_UserData->setAttribute("CurrentAnim", m_newAnim);
	if((dynamic_cast<CAnimatedBillboardSceneNode*>(obj->m_SceneNode))->m_spriteInstance)
		(dynamic_cast<CAnimatedBillboardSceneNode*>(obj->m_SceneNode))->m_spriteInstance->SetAnim(m_newAnim);
	editor->setSelectedNode(obj->m_SceneNode);
	ui->createSelectedObjectProperties();
}

void CSetSpriteAnimCommand::undo()
{
	CSceneObject* obj = editor->getSceneObjectFromID(m_id);
	obj->m_UserData->setAttribute("CurrentAnim", m_oldAnim);
	if((dynamic_cast<CAnimatedBillboardSceneNode*>(obj->m_SceneNode))->m_spriteInstance)
		(dynamic_cast<CAnimatedBillboardSceneNode*>(obj->m_SceneNode))->m_spriteInstance->SetAnim(m_oldAnim);
	editor->setSelectedNode(obj->m_SceneNode);
	ui->createSelectedObjectProperties();
}
